package cn.edu.bzu.ie.sf.mealsmisweb.controller;

import cn.edu.bzu.ie.sf.mealsmisweb.common.MyUtils;
import cn.edu.bzu.ie.sf.mealsmisweb.entity.Disinfection;
import cn.edu.bzu.ie.sf.mealsmisweb.service.DisinfectionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @version v1.0.0
 * @Package : cn.edu.bzu.ie.sf.mealsmisweb.controller
 * @Description : 饮食卫生消毒控制器类
 * @Create on : 2021/8/15 18:43
 * @author: Feng Song 宋锋
 **/
@RestController
@RequestMapping(value = "/disinfection")
public class DisinfectionController {
    @Autowired
    private DisinfectionService disinfectionService;

    /**
     * 获取某个部门下某个周的所有饮食卫生消毒记录信息，用于大队用户的管理
     * @param pid 父部门id
     * @return List<Disinfection> 集合实例
     */
    @RequestMapping(value = "/getDisinfectionListByPidAndWeek")
    public List<Disinfection> getDisinfectionListByPidAndWeek(int pid){
        String year = MyUtils.getYearStrFromNow();
        int week = MyUtils.getWeekNumOfYear();
        return disinfectionService.getDisinfectionListByPidAndWeek(pid, year, week);
    }

    /**
     * 获取某个部门下的某个分页数据
     * @param depid 部门id
     * @param pageNum 当前的页码
     * @param pageSize 每个页面的计算条数
     * @return PageResult 集合实例 分页信息
     */
    @RequestMapping(value = "/getDisinfectionPageListByDepid")
    public Map<String, Object> getDisinfectionPageListByDepid(int depid, int pageNum, int pageSize){
        Map<String, Object> map = new HashMap<>();
        long count = disinfectionService.getCountByDepid(depid);
        map.put("total", count);
        List<Disinfection> list = disinfectionService.getDisinfectionPageListByDepid(depid, (pageNum-1)*pageSize, pageSize);  // 列表集合数据
        map.put("list", list);
        return map;
    }

    /**
     * 获取载个部门某个周的饮食卫生消毒记录信息，用于中队用户的上传与维护
     * 也可以用于支队与大队用户的查询
     * @param depid 部门id
     * @return Disinfection 实例
     */
    @RequestMapping(value = "/getDisinfectionByDepidAndWeek")
    public Disinfection getDisinfectionByDepidAndWeek(int depid){
        String year = MyUtils.getYearStrFromNow();
        int week = MyUtils.getWeekNumOfYear();
        return disinfectionService.getDisinfectionByDepidAndWeek(depid, year, week);
    }

    /**
     * 添加饮食卫生消毒记录信息
     * @param item 要添加的饮食卫生消毒记录信息，无id信息
     * @return  Disinfection  带有id的实体
     */
    @PostMapping(value = "/addDisinfection")
    public Disinfection addDisinfection(Disinfection item){
        item.setWeek(MyUtils.getWeekNumOfYear());     // 当前所在的周数
        item.setWeekstr(MyUtils.getWeekStrOfNow());   // 时间段
        item.setCreate_dt(MyUtils.getDateTimeStrFromNow());   // 创建时间
        item.setYear(MyUtils.getYearStrFromNow());
        int iRow = disinfectionService.addDisinfection(item);
        if(iRow>0){
            return item;
        }else{
            return null;
        }
    }

    /**
     * 修改饮食卫生消毒记录信息
     * @param item 要修改的饮食卫生消毒记录信息
     * @return int 影响的行数
     */
    @PostMapping(value = "/editDisinfection")
    public  Disinfection  editDisinfection(Disinfection item){
        item.setCreate_dt(MyUtils.getDateTimeStrFromNow());   // 修改创建时间
        int iRow = disinfectionService.editDisinfection(item);
        if(iRow>0){
            return item;
        }else{
            return null;
        }
    }

    /**
     * 删除饮食卫生消毒记录信息
     * @param id 要删除对象的id
     * @return int 影响的行数
     */
    @RequestMapping(value = "/delDisinfection")
    public int delDisinfection(int id){
        return disinfectionService.delDisinfection(id);
    }
}
